require "test/unit";

class Array
  
  def sum
    inject(0){|sum, n| sum + n;};
  end
  
end

class ChangeError < StandardError; end

def make_change(amount, coins=[25, 10, 5, 1])
  change = coins.sort.reverse.inject([]) do |change, coin|
    change << coin until change.sum + coin > amount;
    change;
  end
  raise ChangeError unless change.sum == amount;
  change;
end

class ChangeTest < Test::Unit::TestCase
  
  def test_should_raise_error_when_change_is_impossible
    assert_raise(ChangeError) do
      make_change(23, [10, 5]);
    end
  end
  
  def test_should_accept_diferent_coins
    assert_equal [17, 17, 4], make_change(38, [17, 4]);
    assert_equal [52], make_change(52, [52]);
  end
  
  def test_should_give_minimun_number_of_coins
    assert_equal [25, 10, 1, 1], make_change(37);
    assert_equal [1, 1, 1], make_change(3);
    assert_equal [3, 3], make_change(6, [3]);
  end
  
end